// pages/finish/finish.js
const app = getApp();
var utils = require('../../utils/util.js');
const ctx = wx.createCanvasContext("bgCanvas"); //创建一个全局的canvas绘图上下文
const ctx2 = wx.createCanvasContext("runCanvas");
let mytime = "";
var n = "";
var w = "";
var h = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //score: 80,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.init();

    let that = this;
    wx.getStorage({
      key: 'record',
      success(res) {
        var sum = 0;
        var num = 0;
        for(var i = 0;i<res.data.length;i++){
          if(res.data[i].class == '单选' || res.data[i].class == '多选'){
            sum+=1;
            var str = '';
            var youAnswer = '';
            if (res.data[i].xa){
              str = str + "&" + res.data[i].xa;
              res.data[i].xa = res.data[i].answer.indexOf(res.data[i].optiona) == -1 ? 3 : 2;
              youAnswer = youAnswer + 'A'
            }else{
              res.data[i].xa = res.data[i].answer.indexOf(res.data[i].optiona) == -1?1:2 
            }
            if (res.data[i].xb) {
              str = str + "&" + res.data[i].xb;
              res.data[i].xb = res.data[i].answer.indexOf(res.data[i].optionb) == -1 ? 3 : 2;
              youAnswer = youAnswer + 'B'
            }else{
              res.data[i].xb = res.data[i].answer.indexOf(res.data[i].optionb) == -1 ? 1 : 2
            }
            if (res.data[i].xc) {
              str = str + "&" + res.data[i].xc;
              res.data[i].xc = res.data[i].answer.indexOf(res.data[i].optionc) == -1 ? 3 : 2;
              youAnswer = youAnswer + 'C'
            }else{
              res.data[i].xc = res.data[i].answer.indexOf(res.data[i].optionc) == -1 ? 1 : 2
            }
            if (res.data[i].xd) {
              str = str + "&" + res.data[i].xd;
              res.data[i].xd = res.data[i].answer.indexOf(res.data[i].optiond) == -1 ? 3 : 2;
              youAnswer = youAnswer + 'D'
            }else{
              res.data[i].xd = res.data[i].answer.indexOf(res.data[i].optiond) == -1 ? 1 : 2
            }
            if(str.substr(0,1) == '&'){
              str = str.substr(1)
            }
            if(str == res.data[i].answer){
              num+=1
            }
            if(str && str != res.data[i].answer){
              that.init(res.data[i].id,str)
            }
            res.data[i].str = str
            res.data[i].youAnswer = youAnswer
          }
        }
        wx.setStorage({
          key: 'record',
          data: res.data
        })
        that.setData({
          course: res.data,
          score:(num/sum)*100
        })
        let allSrc = 0.015 * that.data.score; //应该绘制的弧度
        let src = allSrc / 100 //计算出每个间隔应该绘制多少弧度。 
        that.setData({
          src: src,
          allSrc: allSrc
        });

        clearInterval(mytime);
        mytime = setInterval(that.run, 10)
      }
    })
    that.setData({
      imgPath: app.d.imgPath,
      lianxi: app.d.lianxi == 1 ? '历年题库' : app.d.lianxi == 2 ?'考前压题':'智能题库'
    })
    
  },
  run(e) {
    let that = this;
    let src = that.data.src; //每个间隔所需绘制的弧度
    let allSrc = that.data.allSrc; //总共需要绘制的弧度
    n++;
    if (src * n > allSrc) {
      clearInterval(mytime); //如果绘制完成，停掉计时器，绘制结束
      n = 0;
      return;
    }
    // console.log(n);
    let grade = Math.round(src * n / 1.5 * 100); //百分数
    ctx2.arc(w, h, w - 8, 0.75 * Math.PI, (0.75 + src * n) * Math.PI); //每个间隔绘制的弧度
    ctx2.setStrokeStyle("#80E0FE");
    ctx2.setLineWidth("8");
    ctx2.setLineCap("round");
    ctx2.stroke();
    ctx2.beginPath();
    ctx2.setFontSize(20); //注意不要加引号
    ctx2.setFillStyle("#80E0FE");
    ctx2.setTextAlign("center");
    ctx2.setTextBaseline("middle");
    ctx2.fillText(grade + "%", w, h);


    ctx2.draw();
  },
  init: function (id,answer) {
    var that = this;
    var sucFunc = function (that, res) {
      //console.log(res.data)
      
    }
    var lianxi = app.d.lianxi == 3 ? 'questionbank_id' : 'paperquestions_id'
    var data = { openid: app.d.openid,id: id,answer:answer,lianxi:lianxi}
    utils.wxRequest(that, app.d.path, "Profession/errorBank", data, sucFunc);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.createSelectorQuery().select('#canvas-one').boundingClientRect(function (rect) {//监听canvas的宽高
      w = parseInt(rect.width / 2); //获取canvas宽的的一半
      h = parseInt(rect.height / 2); //获取canvas高的一半，
      //获取宽高的一半是为了便于找到中心点

      ctx.arc(w, h, w - 8, 0.75 * Math.PI, 2.25 * Math.PI); //绘制圆形弧线
      ctx.setStrokeStyle("#dddddd"); //设置填充线条颜色
      ctx.setLineWidth("8");     //设置线条宽度
      ctx.setLineCap("round");        //设置线条端点样式
      ctx.stroke();     //对路径进行描边，也就是绘制线条。

      ctx.draw();       //开始绘制
    }).exec()

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  tiao: function (e) {
    var that = this;
    var cateId = e.currentTarget.dataset.id;

    wx.redirectTo({
      url: '/pages/questions/questions?cateId=' + cateId
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  jiex:function(){
    wx.redirectTo({
      url: '/pages/questions/questions'
    })
  }
})